package com.zhudi.develop_demo_as.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.zhudi.develop.view.ZhudiPullListView;
import com.zhudi.develop_demo_as.R;
import com.zhudi.develop_demo_as.TemplateFragment;
import com.zhudi.develop_demo_as.adapter.StoreAdapter;
import com.zhudi.develop_demo_as.entity.ModelTest;
import com.zhudi.develop_demo_as.message.MessageParameter;
import com.zhudi.develop_demo_as.network.NetworkService;
import com.zhudi.develop_demo_as.network.URLs;
import com.zhudi.develop_demo_as.widget.DialogCancelInterface;
import com.zhudi.develop_demo_as.widget.ProgressHUD;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class StoreFragment extends TemplateFragment {
	@Bind(R.id.tvTitle)
	TextView tvTitle;
	@Bind(R.id.lvStore)
	ZhudiPullListView lvStore;

	private MessageParameter mp = null;
	private StoreAdapter adapter;
	private List<ModelTest> datasList = new ArrayList<>();

	@Override
	protected String prepareURL(MessageParameter mp) throws Exception {
		if (mp.activityType == 0){
			return URLs.BAIDU;
		}
		return null;
	}

	@Override
	protected int getLayoutView() {
		return R.layout.fragment_store;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initView();
		initData();
	}


	private void initView() {
		tvTitle.setText("商家");
		setData(true);
		adapter = new StoreAdapter(getActivity(),datasList);
		lvStore.setAdapter(adapter);

		ProgressHUD pd = ProgressHUD.create(getActivity())
				.setStyle(ProgressHUD.Style.SPIN_INDETERMINATE)
				.setLabel("请稍等")
				.setBackCancelListener(new DialogCancelInterface() {
					@Override
					public void backCancel() {
						System.out.println("MainActivity------>取消");
					}
				})
				.setCancellable(true);
		pd.show();
	}

	private void initData() {
//		mp = new MessageParameter();
//		mp.activityType = 0;
//		processThread(mp, null);
		String url = "http://www.baidu.com";
		NetworkService<String> service = new NetworkService<>(null, new NetworkService.NetworkListener<String>() {
			@Override
			public void onReceiveResult(String resultData) {
				if (resultData != null) {
				}
			}

			@Override
			public void onFailure(int code, String msg) {
			}
		});
		service.configContext(getActivity());
		service.configAnalyze(null);
		service.getSubmit(url);
	}

	private void setData(Boolean flag){
		if (flag){
			datasList.clear();
		}
		for (int i = 0; i < 10; i++){
			ModelTest test = new ModelTest();
			datasList.add(test);
		}
	}
}
